<%@ page language="java" import="com.alibaba.fastjson.JSONArray" pageEncoding="UTF-8" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="okhttp3.*" %>
<%@ page import="org.apache.axis.encoding.Base64" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.interfaces.zhlx.duss.utils.PropUtil" %>
<%@ page import="java.util.UUID" %>
<%@ page import="java.util.concurrent.TimeUnit" %>
<%--建模查询调用HCM获取工资单接口--%>
<%
    JSONObject jsonData = new JSONObject();
    BaseBean baseBean = new BaseBean();
    RecordSet rs = new RecordSet();
    String message = "";
    String status = "success";
    JSONArray data = new JSONArray();
    String id = Util.null2String(request.getParameter("id"));
    try {
        rs.executeQuery("select gh,replace(ny,'-','') ny  from uf_yg_xch_jm where id=?", id);
        rs.next();
        String gh = Util.null2String(rs.getString("gh"));
        String ny = Util.null2String(rs.getString("ny"));
        JSONObject paramJson = new JSONObject();
        paramJson.put("PERNR", gh);
        paramJson.put("YEARMONTH_BUDAT", ny);
        baseBean.writeLog(">>>>>getGzdByHCM_Ajax.jsp>>>>>paramJson=" + paramJson);
        JSONObject resultJson = handleOKHttpRequest("EC207", paramJson);
        baseBean.writeLog(">>>>>getGzdByHCM_Ajax.jsp>>>>>resultJson=" + resultJson);
        String code = Util.null2String(resultJson.getString("code"));
        if ("success".equals(code)) {
            String output = Util.null2String(resultJson.getString("OUTPUT"));
            JSONObject outputJson = JSONObject.parseObject(output);
            String msgType = outputJson.getString("MSG_TYPE");
            if ("S".equalsIgnoreCase(msgType)) {
                String returnDataStr = outputJson.getString("DATA");
                data = JSONArray.parseArray(returnDataStr);
            } else {
                message = outputJson.getString("MSG_TEXT");
                status = "failture";
            }
        } else {
            message = Util.null2String(resultJson.getString("message"));
            status = "failture";
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    jsonData.put("status", status);
    jsonData.put("data", data);
    jsonData.put("message", message);
    out.print(jsonData.toJSONString());
%>
<%!
    private static final JSONObject propVal = PropUtil.getPropVal("zhlxhcm");
    private static final String URL = propVal.getString("url")+"/RESTAdapter/ecthcm/interface";
    private static final String USERNAME = propVal.getString("username");
    private static final String PASSWORD = propVal.getString("password");

    /**
     * 请求处理器
     */
    public JSONObject handleOKHttpRequest(String type, JSONObject dataJson) {
        String fullDate = DateUtil.getFullDate();
        String curdate = fullDate.substring(0, 10).replace("-", "");
        String curtime = fullDate.substring(11, 19).replace(":", "");
        JSONObject headerJson = new JSONObject();
        headerJson.put("SYSID", "EC");
        headerJson.put("BUSINESS", type);
        headerJson.put("BUSNKEY", "");
        headerJson.put("TECHKEY", getUUIDStr().toUpperCase());
        headerJson.put("DATE", curdate);
        headerJson.put("TIME", curtime);
        JSONObject paramsJson = new JSONObject();
        JSONObject inuptJson = new JSONObject();
        inuptJson.put("HEADER", headerJson);
        inuptJson.put("DATA", dataJson);
        paramsJson.put("INPUT", inuptJson.toJSONString());
        return sendOKHttpRequest(paramsJson);
    }

    private JSONObject sendOKHttpRequest(JSONObject params) {
        JSONObject resultJson = new JSONObject();
        String code = "fail";
        String message = "";
        try {
            String authorization = " Basic " + Base64.encode((USERNAME + ":" + PASSWORD).getBytes());
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, params.toJSONString());
            Request request = new Request.Builder()
                    .url(URL)
                    .method("POST", body)
                    .addHeader("Authorization", authorization)
                    .addHeader("Content-Type", "application/json")
                    .build();
            Response response = client.newCall(request).execute();
            String result = Util.null2String(response.body().string());
            if (!"".equals(result)) {
                resultJson = JSONObject.parseObject(result);
                code = "success";
            } else {
                message = "response result is null";
            }
        } catch (Exception e) {
            message = "response timeout";
            e.printStackTrace();
        }
        resultJson.put("code", code);
        resultJson.put("message", message);
        return resultJson;
    }

    private String getUUIDStr() {
        String uuid = UUID.randomUUID().toString().toUpperCase();
        return uuid.replace("-", "");
    }

%>



